package modele.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import modele.ConnectionManager;
import data.Detaillant;
import data.Succursale;

public class DaoSuccursale extends DAO<Succursale>{

	@Override
	public Succursale find(int id) {
		String sql = "SELECT * FROM succursale WHERE idSuccursale = ?";
		Succursale s = null;
		PreparedStatement requete;
		try {
			requete = prepareStatement(sql);			
			requete.setInt(1, id);	

			ResultSet result = requete.executeQuery();	

			if(result != null && result.next()) {
				s= new Succursale();
				s.setNom(result.getString("NOM"));
				s.setAdresse(result.getString("ADRESSE"));
				s.setCodePostal(result.getString("CODEPOSTAL"));
				s.setLatitude(result.getFloat("LATITUDE"));
				s.setLongitude(result.getFloat("LONGITUDE"));
				s.setDescription(result.getString("DESCRIPTION"));		
				s.setId(result.getInt("DETAILLANT_idDETAILLANT"));
				s.setIdDetaillant(result.getInt("idSUCCURSALE"));

			}

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return s;
	}

	public ArrayList<Succursale> findFromDet(int idDet) {
		String sql = "SELECT * FROM succursale WHERE DETAILLANT_idDETAILLANT = ?";
		ArrayList<Succursale> succursales = new ArrayList<Succursale>();
		PreparedStatement requete;
		try {
			requete = prepareStatement(sql);			
			requete.setInt(1, idDet);	

			ResultSet result = requete.executeQuery();	

			while(result != null && result.next()) {
				Succursale s;
				s= new Succursale();
				s.setNom(result.getString("NOM"));
				s.setAdresse(result.getString("ADRESSE"));
				s.setCodePostal(result.getString("CODEPOSTAL"));
				s.setLatitude(result.getFloat("LATITUDE"));
				s.setLongitude(result.getFloat("LONGITUDE"));
				s.setDescription(result.getString("description"));	
				s.setId(result.getInt("idSUCCURSALE"));
				s.setIdDetaillant(result.getInt("DETAILLANT_idDETAILLANT"));
				succursales.add(s);
			}

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return succursales;
	}

	@Override
	public void create(Succursale s) {
		String sql = "INSERT INTO succursale (nom,adresse,codepostal,latitude,longitude,description,DETAILLANT_idDETAILLANT) VALUES (?,?,?,?,?,?,?)";
		PreparedStatement requete;
		try {

			//PreparedStatement prest = ConnectionManager.getConnection().prepareStatement(sql);
			requete = prepareStatement(sql);			
			requete.setString(1, s.getNom());	
			requete.setString(2, s.getAdresse());
			requete.setString(3, s.getCodePostal());
			requete.setFloat(4, s.getLatitude());
			requete.setFloat(5, s.getLongitude());
			requete.setString(6, s.getDescription());
			requete.setInt(7, s.getIdDetaillant());

			requete.execute();

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(Succursale s) {
		String sql = "UPDATE succursale SET nom=?,adresse=?,codepostal=?,latitude=?,longitude=?,description=? WHERE idSuccursale=? ";
		PreparedStatement requete;
		try {

			//PreparedStatement prest = ConnectionManager.getConnection().prepareStatement(sql);
			requete = prepareStatement(sql);			
			requete.setString(1, s.getNom());	
			requete.setString(2, s.getAdresse());
			requete.setString(3, s.getCodePostal());
			requete.setFloat(4, s.getLatitude());
			requete.setFloat(5, s.getLongitude());
			requete.setString(6, s.getDescription());

			requete.setInt(7, s.getId());

			requete.execute();

			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void delete(Succursale s) {
		String sql = "Delete from succursale where idSUCCURSALE = ?";
		PreparedStatement requete;
		try {			
			requete = prepareStatement(sql);			
			requete.setInt(1, s.getId());
			requete.execute();
			requete.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
